#include <rtthread.h>
#include "st_led.h"
#include "drv_gpio.h"

//#define DRV_DEBUG
#define LOG_TAG           "led.drv"
#include <drv_log.h>


BoardLED boardLED;
static void _turnOnBoardLED(LEDNumber led)
{
	switch (led)
    {
    case LED2A:
        rt_pin_write(LED2A_PIN_INDEX, PIN_HIGH);
        break;
	case LED2B:
		rt_pin_write(LED2B_PIN_INDEX, PIN_HIGH);
        break;
    default:
        break;
    }
}

static void _turnOffBoardLED(LEDNumber led)
{
    switch (led)
    {
    case LED2A:
        rt_pin_write(LED2A_PIN_INDEX, PIN_LOW);	
        break;
	case LED2B:
		rt_pin_write(LED2B_PIN_INDEX, PIN_LOW);	
        break;
    default:
        break;
    }
}

static void _turnOffLED2A(void)
{
	rt_pin_write(LED2A_PIN_INDEX, PIN_LOW);	
}

static void _turnOffLED2B(void)
{
	rt_pin_write(LED2B_PIN_INDEX, PIN_LOW);	
}



static void _turnOnLED2A(void)
{
	rt_pin_write(LED2A_PIN_INDEX, PIN_HIGH);	
}

static void _turnOnLED2B(void)
{
	rt_pin_write(LED2B_PIN_INDEX, PIN_HIGH);	
}



static void _setLEDstate(LEDNumber lednum, LEDState state)
{
	boardLED.ledstate[lednum] = state;
}


//static void led2A_thread_entry(void *parameter)
//{
//    while (1)
//    {
//		boardLED.turnOnLED2A();
//        rt_thread_delay(RT_TICK_PER_SECOND / 2);

//		boardLED.turnOffLED2A();
//        rt_thread_delay(RT_TICK_PER_SECOND / 2);
//    }
//}

static void led2A_thread_entry(void *parameter)
{
    while (1)
    {
		switch(boardLED.ledstate[LED2A])
		{
			case LED_ON:
				rt_pin_write(LED2A_PIN_INDEX, PIN_HIGH);	
				rt_thread_delay(RT_TICK_PER_SECOND);
				break;
			
			case LED_OFF:
				rt_pin_write(LED2A_PIN_INDEX, PIN_LOW);	
				rt_thread_delay(RT_TICK_PER_SECOND);
				break;
			
			case LED_SLOW:
					boardLED.turnOnLED2A();
					rt_thread_delay(RT_TICK_PER_SECOND / 2);

					boardLED.turnOffLED2A();
					rt_thread_delay(RT_TICK_PER_SECOND / 2);
					break;	
			
			case LED_MIDDLE:
					boardLED.turnOnLED2A();
					rt_thread_delay(RT_TICK_PER_SECOND / 4);

					boardLED.turnOffLED2A();
					rt_thread_delay(RT_TICK_PER_SECOND / 4);
					break;					
			
			case LED_QUICK:
					boardLED.turnOnLED2A();					
					rt_thread_delay(RT_TICK_PER_SECOND / 8);

					boardLED.turnOffLED2A();
					rt_thread_delay(RT_TICK_PER_SECOND / 8);	
					break;	
			
			default:
					break;	
		}			
    }
}


int boardLEDInit(void)
{
	rt_thread_t led2A_thread;

	rt_pin_mode(LED2A_PIN_INDEX, PIN_MODE_OUTPUT);
	rt_pin_mode(LED2B_PIN_INDEX, PIN_MODE_OUTPUT);
	
	rt_pin_write(LED2A_PIN_INDEX, PIN_LOW);
	rt_pin_write(LED2B_PIN_INDEX, PIN_LOW);
	
	boardLED.ledstate[LED2A] = LED_SLOW;
	boardLED.ledstate[LED2B] = LED_ON;
	
	boardLED.turnOn 		= _turnOnBoardLED;
	boardLED.turnOff 		= _turnOffBoardLED;
	boardLED.turnOffLED2A 	= _turnOffLED2A;
	boardLED.turnOffLED2B	= _turnOffLED2B;
	
	boardLED.turnOnLED2A	= _turnOnLED2A;
	boardLED.turnOnLED2B	= _turnOnLED2B;

	boardLED.setLEDstate    = _setLEDstate;
	
	/* init led thread */
    led2A_thread = rt_thread_create("led2A",
                                  led2A_thread_entry, RT_NULL,
                                  215, 2, 20);

    if (led2A_thread != RT_NULL )
	{
        rt_thread_startup(led2A_thread);	
		return RT_EOK;
	}
    else
	{
        rt_kprintf("start led thread fail\n");
		return RT_ERROR;
	}
}

INIT_DEVICE_EXPORT(boardLEDInit);



#ifdef FINSH_USING_MSH
//static int led_flash_frequency(int argc, char **argv)
//{
//    if (argc == 1)
//    {
//        rt_kprintf("led state is %d",led_state_flag);
//    }
//    else
//    {
//		rt_uint8_t state = argv[1][0]-0x30;
//		boardLED.setLEDstate(state);  
//		return -1;
//    }

//    return 0;
//}

//MSH_CMD_EXPORT(led_flash_frequency, set led flash frequency);
#endif /* FINSH_USING_MSH */









